$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true,
			labelPosition:'left',
			queryIng : false, //正在查询
			queryData:{activeFlag:'1',page:1,rows:10,orderColum:"create_date"}, //查询条件
			datagrid:[], //数据列表数据
			totalNum:0, //分页控件配置
			data:[],
			defaultCheckedKeys:[],
			addData:{},
			selectedRow:null,
			isEdit:false,
			rules:{
				expressOrderCode: [{ required: true, message: '该项必填', trigger: 'change' }],
				expressCompanyCode: [{ required: true, message: '该项必填', trigger: 'change' }],
			},
			dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
				,expressVisible:false
			},
			size:"mini",
			expressData:{
				expressCompanyCode:"",
				expressCompanyName:"",
				expressOrderCode:""
			},
			expressOptions:[],
			orderStatusOpt:[
				{label:"待付款",value:"1"},
				{label:"已付款",value:"2"},
				{label:"已完成",value:"3"},
				{label:"已取消",value:"4"},
			],
			sendStatusOpt:[
				{label:"未发货",value:"0"},
				{label:"已发货",value:"1"}
			]
		},
		methods: {
			formatSendStatus:function(e){
				for(var i=0;i<app.sendStatusOpt.length;i++){
					if(app.sendStatusOpt[i].value==e){
						return app.sendStatusOpt[i].label;
					}
				}
			},
			init:function () {
				var vm = this
				vm.query();
				this.queryExpressCompany();
			},
			search:function(){
				//点击查询按钮，默认查询第一页。
				this.queryData.page=1;
				this.query();
			},
			query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/mall/order/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			queryExpressCompany:function () {
				console.log(SERVICE.SERVICE_BASIC+'/basic/baseInfoAction/combox');
				$.ajax({
					url :SERVICE.SERVICE_BASIC+'/basic/baseInfoAction/combox',
					data :{
						parentCode:'A01'
					},
					dataType : 'json',
					success : function(r) {
						app.expressOptions = r;
					}
				});
			},
			addItem:function(){
				app.openDialog();
			},
			deleteItem:function(row){
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
			        		url : staticURL+"/mall/order/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
				
			},
			editItem:function(row){
				app.openDialog(row);
			},
			handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			},
			handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			},
			handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			},
			resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    },
			submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						$.ajax({
							url : staticURL+'/mall/order/sendOrder',
							data : app.expressData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.expressVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			},
			openDialog:function(param){
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
					
					$.ajax({
		        		url : staticURL+"/mall/orderGoods/listAll?orderId="+app.addData.id,
						dataType : 'json',
						async: false,
						success : function(r) {
							app.addData.itemList = r.data;
						}
					});
				}else{
					app.isEdit = false;
					app.addData = {
							orderNum:1
							};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
				
			},
			closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
			},
			closeDialog2:function(){
				app.dialogConfig.expressVisible=false;
				app.resetForm('dialogForm');
			},
			datagridSelect:function(row, event, column){
				app.selectedRow = row;
			},
			formatStepStatus:function(item) {
		        if (item.orderStateCode == 1) {
		          //待发货
		          return 1;
		        } else if (item.orderStateCode == 2 && item.sendStatus==0) {
		          //等待发货
		          return 2;
		        } else if (item.orderStateCode == 2 && item.sendStatus==1) {
		          //发货
		          return 3;
		        } else if (item.orderStateCode == 3 && item.ifEvaluate==0) {
		          //评价
		          return 4;
		        } else if (item.orderStateCode == 3 && item.ifEvaluate==1) {
		          //已完成
		          return 5;
		        }else {
		          //待付款、已关闭、无限订单
		          return 1;
		        }
		    },
		    sendGoods:function(){
		    	this.dialogConfig.expressVisible = true;
		    	this.expressData.id = this.addData.id;
		    	this.expressData.expressCompanyCode = this.addData.expressCompanyCode;
		    	this.expressData.expressCompanyName = this.addData.expressCompanyName;
		    	this.expressData.expressOrderCode = this.addData.expressOrderCode;
		    	this.addData.sendStatus = '1';
		    },
		    expressChange:function(code){
		    	var that = this;
		    	for(var i=0;i<that.expressOptions.length;i++){
		    		console.log(that.expressOptions[i].itemCodeCopy , code);
		    		if(that.expressOptions[i].itemCodeCopy == code){
		    			this.expressData.expressCompanyCode = that.expressOptions[i].itemCodeCopy;
		    			this.expressData.expressCompanyName = that.expressOptions[i].itemName;
		    			break;
		    		}
		    	}
		    }
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/mall/order/edit'
				}else{
					return staticURL+'/mall/order/add'
				}
			},
			columnWidth:function(){

				let w = $(window).width();   

				w = w - 48-48-50-150;
				
				return Math.floor(w/5);
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});